import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../views/Login/indexLogin.vue')
  },
 
  {
    path: '/home',
    redirect:"/welcome",
    meta:{
      auth:true
    },
    component: () => import('../views/Home/indexHome.vue'),
    children:[
      {
        path:"/users",
        component:()=>import('../views/users/users.vue')
      },
      {
        path:"/welcome",
        component:()=>import('../views/welcome/welcome.vue')
      },
      {
        path:"/roles",
        component:()=>import('../views/quanxian/roles.vue')
      },
      {
        path:"/rights",
        component:()=>import('../views/quanxian/rights.vue')
      },
      {
        path:"/goods",
        component:()=>import('../views/stores/goods.vue')
      },
      {
        path:"/params",
        component:()=>import('../views/stores/params.vue')
      },
      {
        path:"/categories",
        component:()=>import('../views/stores/categories.vue')
      },
      {
        path:"/Addlist",
        component:()=>import('../views/stores/Addlist.vue')
      },
      {
        path:"/orders",
        component:()=>import('../views/orders/orders.vue')
      },
      {
        path:"/reports",
        component:()=>import('../views/reports/reports.vue')
      },

    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  const arr = to.matched.some(item=>{
    return item.meta.auth
  })
  if(arr){
    const flag = sessionStorage.getItem("token")
    if(!flag){
      next({
        path:"/login"
      })
    }else{
      next()
    }
    }else{
      next()
  }
})

export default router
